//
//  DBCache.m
//  AFNetworking
//
//  Created by baboy on 2019/2/13.
//

#import <Foundation/Foundation.h>
#import "DBCache.h"
#import "FileCache.h"

#define CacheSqlQuery       @"select value from cache where domain=? and key=?"
#define CacheSqlRmForKey    @"delete from cache where domain=? and key=?"
#define CacheSqlInsert      @"INSERT INTO cache (domain,key, value, updatetime,readtime) VALUES (?,?,?,?,?)"
#define CacheSqlUpdate      @"update cache set key=?,val=?,updatetime=?"
#define DefDomain           @"G"

@implementation DBCache
static NSRecursiveLock *db_cache_lock;
+ (void)initialize{
    NSLog(@"DBCache initialize...");
    if(!db_cache_lock){
        db_cache_lock = [[NSRecursiveLock alloc] init];
    }
}
+ (BOOL) setValue:(id)val forKey:(NSString *)key domain:(NSString *)domain{
    NSLog(@"db_cache_lock:%@",db_cache_lock);
    [db_cache_lock lock];
    NSDate *d = [NSDate date];
    if (  val && ([val isKindOfClass:[NSArray class]] || [val isKindOfClass:[NSDictionary class]] ) ) {
        SEL jsonSelector = NSSelectorFromString(@"jsonString");
        if ([val respondsToSelector:jsonSelector]) {
            IMP imp = [val methodForSelector:jsonSelector];
            id (*_jsonString)(id, SEL) = (void *)imp;
            val = _jsonString(val, jsonSelector);
        }
    }
    if (![val isKindOfClass:[NSString class]]) {
        val = [val description];
    }
    BOOL flag =[[self db] executeUpdate:CacheSqlInsert,domain,key,val,d,d];
    [db_cache_lock unlock];
    return flag;
}
+ (BOOL) removeForKey:(NSString *)key{
    [db_cache_lock lock];
    BOOL flag =[[self db] executeUpdate:CacheSqlRmForKey,DefDomain,key];
    [db_cache_lock unlock];
    return flag;
}
+ (BOOL) setValue:(id)val forKey:(NSString *)key{
    return [self setValue:val forKey:key domain:DefDomain];
}
+ (BOOL) setInt:(int)val forKey:(NSString *)key{
    [self setValue:[NSNumber numberWithInt:val] forKey:key];
    return NO;
}
+ (BOOL) setFloat:(float)val forKey:(NSString *)key{
    [self setValue:[NSNumber numberWithFloat:val] forKey:key];
    return NO;
}
+ (BOOL) setDouble:(double)val forKey:(NSString *)key{
    [self setValue:[NSNumber numberWithDouble:val] forKey:key];
    return NO;
}

+ (NSString *) valueForKey:(NSString *)key{
    return [self valueForKey:key domain:DefDomain];
}
+ (NSString *) valueForKey:(NSString *)key domain:(NSString *)domain{
    FMDatabase *db = [self db] ;
    NSString *val = [db stringForQuery:CacheSqlQuery,domain,key];
    if ((NSNull *)val == [NSNull null]) {
        val = nil;
    }
    return val;
}

+ (int) intForKey:(NSString *)key{
    return [[self valueForKey:key] intValue];
}
+ (float) floatForKey:(NSString *)key{
    return [[self valueForKey:key] floatValue];
}
+ (float) doubleForKey:(NSString *)key{
    return [[self valueForKey:key] doubleValue];
}

+ (NSDictionary *)dictForKey:(NSString *)key{
    NSString *v = [self valueForKey:key];
    if (!v) {
        return nil;
    }
    NSData *d = [v dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:d options:0 error:nil];
    return json;
}
+ (NSArray *)arrayForKey:(NSString *)key{
    NSString *v = [self valueForKey:key];
    NSData *d = [v dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:d options:0 error:nil];
    return json;
}
+ (void) clear{
    
}
+ (void) loadFile:(NSString *)fn{
    NSString *fp = getBundleFilePath([NSString stringWithFormat:@"%@.plist", fn], nil);
    NSDictionary *d = [NSDictionary dictionaryWithContentsOfFile:fp];
    for (NSString *k in [d allKeys]) {
        NSString *v = [d valueForKey:k];
        [DBCache setValue:v forKey:k];
    }
}
@end
